package edu.avans.hartigehap.service.jpa;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

import edu.avans.hartigehap.domain.EmptyBillException;
import edu.avans.hartigehap.domain.StateException;
import edu.avans.hartigehap.domain.WebDiscountInterface;
import edu.avans.hartigehap.domain.WebDish;
import edu.avans.hartigehap.domain.WebMenuItem;
import edu.avans.hartigehap.domain.WebOrder;
import edu.avans.hartigehap.domain.WebPizzaFactory;
import edu.avans.hartigehap.repository.WebDishRepository;
import edu.avans.hartigehap.repository.WebMenuItemRepository;
import edu.avans.hartigehap.repository.WebMenuItemRepositoryImpl;
import edu.avans.hartigehap.repository.WebOrderRepository;
import edu.avans.hartigehap.service.WebOrderService;

@Service("webOrderService")
@Repository
@Transactional(rollbackFor = StateException.class)
public class WebOrderServiceImpl implements WebOrderService {
	final Logger logger = LoggerFactory.getLogger(WebOrderServiceImpl.class);

	@Autowired
	private WebOrderRepository webOrderRepository;
	@Autowired
	private WebDishRepository webDishRepository;
	@Autowired
	private WebMenuItemRepositoryImpl webMenuItemRepositoryCustom;
	@Autowired
	private WebMenuItemRepository webMenuItemRepository;


	@Transactional(readOnly=true)
	public List<WebOrder> findAll() {
		return Lists.newArrayList(webOrderRepository.findAll());
	}

	@Transactional(readOnly = true)
	public WebOrder findById(Long id) {
		return webOrderRepository.findById(id);
	}


	public WebOrder save(WebOrder webOrder) {
		return webOrderRepository.save(webOrder);
	}


	public void delete(Long id) {
		webOrderRepository.delete(id);
	}

	@Transactional(readOnly=true)
	public Page<WebOrder> findAllByPage(Pageable pageable) {
		return webOrderRepository.findAll(pageable);
	}

	@Transactional(readOnly=true)
	public WebOrder fetchWarmedUp(Long webOrderId) {
		logger.info("(fetchWarmedUp) web order id: " + webOrderId);

		WebOrder webOrder = webOrderRepository.findOne(webOrderId);

		logger.info("web order = " + webOrder.getId());

		return webOrder;
	}


	public Long addOrderItem(WebOrder webOrder, String dishName) {
		Iterable<WebMenuItem> items = webMenuItemRepository.findAll();
		WebMenuItem item = null;
		for (WebMenuItem i : items) {
			if (i.getDishName().equals(dishName)) {
				item = i;
			}
		}
		return WebPizzaFactory.getInstance().addDish(item, webOrder, webDishRepository);
	}


	public void deleteOrderItem(WebOrder webOrder, String dishName) {
		Iterable<WebDish> dishes = webDishRepository.findAll();
		WebDish dish = null;
		for (WebDish d : dishes) {
			if (d.getDishName().equals(dishName)) {
				dish = d;
			}
		}
		webOrder.deleteOrderItem(dish);
	}

	public Long decoratePizza(WebOrder order, WebDish pizza, String toppingName) {
		Iterable<WebMenuItem> toppingItems = webMenuItemRepositoryCustom.findAllToppings();
		WebMenuItem topping = null;
		for (WebMenuItem t : toppingItems) {
			if (t.getDishName().equals(toppingName)) {
				topping = t;
			}
		}
		return WebPizzaFactory.getInstance().addTopping(topping, pizza, order, webDishRepository);
	}

	public WebOrder createNewWebOrder() {
		WebOrder webOrder = new WebOrder();
		webOrder = webOrderRepository.save(webOrder);
		return webOrder;
	}

	@Transactional
	public void submitOrder(WebOrder webOrder) {
		webOrder.changeState();
	}


	public void submitBill(WebOrder webOrder) throws StateException,
	EmptyBillException {

	}

	public void setOrderDiscount(WebOrder webOrder, WebDiscountInterface discount)
	{
		webOrder.setWebDiscountInterface(discount);
		webOrderRepository.save(webOrder);
	}

}
